package com.yupi.springbootinit.manager;

import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.yucongming.dev.client.YuCongMingClient;
import com.yupi.yucongming.dev.common.BaseResponse;
import com.yupi.yucongming.dev.model.DevChatRequest;
import com.yupi.yucongming.dev.model.DevChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
@Slf4j
public class AiManager {


    @Resource
    private YuCongMingClient client;

    /**
     *
     * @param modelId
     * @param message 要发送的消息，不超过1024个字
     * @return
     */
    public String doChat(long modelId,String message){
        if(message.length()>=1024){
            log.error("鱼聪明不支持 消息超过1024字");
            return "";
        }
        DevChatRequest devChatRequest = new DevChatRequest();
        devChatRequest.setModelId(modelId);
        devChatRequest.setMessage(message);
        BaseResponse<DevChatResponse> response = client.doChat(devChatRequest);
        if(response==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"AI 响应错误");
        }
        if(response.getData()==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"AI 响应为空");
        }
        return response.getData().getContent();
    }
}
